package thread.fenjiesuoyufenduansuo;

import java.util.HashSet;
import java.util.Set;

public class DecompossClazz2{
    private final Set<String> allUsers = new HashSet<String>();
    private final Set<String> allComputers = new HashSet<String>();

    public void addUser(String user){ //分解为两把锁

        synchronized (allUsers){
            try {
                Thread.currentThread().sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("添加用户成功:"+user);
            allUsers.add(user);
        }
    }

    public void addComputer(String computer){
        synchronized (allComputers){
            try {
                Thread.currentThread().sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("添加电脑:"+computer+"成功");
            allComputers.add(computer);
        }
    }
}
